package com.flow.engine.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flow.engine.dto.FlowTemplateDTO;
import com.flow.engine.entity.FlowFatherChildNode;
import com.flow.engine.entity.FlowNodeTemplate;
import com.flow.engine.entity.FlowTemplate;
import com.flow.engine.enums.standard.FlowAuditType;
import com.flow.engine.mapper.FlowTemplateMapper;
import com.flow.engine.service.FlowFatherChildNodeService;
import com.flow.engine.service.FlowNodeTemplateService;
import com.flow.engine.service.FlowTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
@Transactional
public class FlowTemplateServiceImpl extends ServiceImpl<FlowTemplateMapper, FlowTemplate> implements FlowTemplateService {

    @Autowired
    private FlowTemplateMapper mapper;

    @Autowired
    private FlowNodeTemplateService flowNodeTemplateService;

    @Autowired
    private FlowFatherChildNodeService flowFatherChildNodeService;

    @Override
    public List<FlowTemplate> queryList(FlowTemplate record) {
        return mapper.selectList(new QueryWrapper<>(record));
    }

    @Override
    public List<FlowTemplate> queryListPage(Integer pageNum, Integer pageSize, FlowTemplate record) {
        Page p = new Page(pageNum, pageSize);
        Page<FlowTemplate> page = mapper.selectPage(p, new QueryWrapper<>(record));
        return page.getRecords();
    }

    @Override
    public List<FlowNodeTemplate> queryNodeList(String userId, Long flowId) {


        return List.of();
    }

    @Override
    @Transactional
    public Boolean createFlowTemplate(FlowTemplateDTO templates) {
        FlowTemplate flowTemplate = new FlowTemplate();
        BeanUtils.copyProperties(templates, flowTemplate);
        flowTemplate.setId(null);
        save(flowTemplate);
        List<FlowNodeTemplate> nodeTemplates = templates.getNodeTemplates();
        Map<Long, FlowNodeTemplate> srcflowNodeTemplateMap = new HashMap<>(nodeTemplates.size());
        nodeTemplates.forEach(item -> {
            item.setFlowTemplateId(flowTemplate.getId());
            srcflowNodeTemplateMap.put(item.getId(), item);
        });
        nodeTemplates.forEach(item -> item.setId(null));
        flowNodeTemplateService.saveBatch(nodeTemplates);

        List<FlowFatherChildNode> flowFatherChildNodes = new ArrayList<>();
        nodeTemplates.forEach(item -> {
            List<Long> nextIds = item.getNextIds();
            List<Long> parentIds = item.getParentIds();
            if (!CollectionUtils.isEmpty(nextIds)) {
                nextIds.forEach(nextId -> flowFatherChildNodes.add(FlowFatherChildNode.builder().nextId(srcflowNodeTemplateMap.get(nextId).getId()).currentId(item.getId()).flowId(item.getFlowTemplateId()).type("flowNodeTemplate").build()));
            }
            if (!CollectionUtils.isEmpty(parentIds)) {
                parentIds.forEach(parentId -> flowFatherChildNodes.add(FlowFatherChildNode.builder().parentId(srcflowNodeTemplateMap.get(parentId).getId()).currentId(item.getId()).flowId(item.getFlowTemplateId()).type("flowNodeTemplate").build()));
            }
        });
        return flowFatherChildNodeService.saveBatch(flowFatherChildNodes);
    }

    public static void main(String[] args) {
        FlowTemplateDTO flowTemplateDTO = new FlowTemplateDTO();
        flowTemplateDTO.setId(500L);
        flowTemplateDTO.setIcon("icon");
        flowTemplateDTO.setScope("scope");
        flowTemplateDTO.setRemark("remark");
        flowTemplateDTO.setInitiator("initiator");
        flowTemplateDTO.setStatus(1);
        flowTemplateDTO.setTitleName("title");

        List<FlowNodeTemplate> nodes = new ArrayList<>();
        List<Long> parentIds = new ArrayList<>();
        parentIds.add(100L);

        List<Long> childIds = new ArrayList<>();
        childIds.add(200L);
        childIds.add(300L);


        FlowNodeTemplate node1 = new FlowNodeTemplate();
        node1.setId(100L);
        node1.setFlowAuditValue("userId");
        node1.setFlowTemplateId(500L);
        node1.setFlowNodeName("开始");
        node1.setCurrentCondition("OR");
        node1.setNextIds(childIds);
        node1.setFlowAuditValue("userId");
        node1.setFlowAuditType(FlowAuditType.DM);
        nodes.add(node1);




        FlowNodeTemplate node2 = new FlowNodeTemplate();
        node2.setId(200L);
        node2.setFlowAuditValue("1userId");
        node2.setFlowTemplateId(500L);
        node2.setFlowNodeName("2开始");
        node2.setCurrentCondition("OR");
        node2.setParentIds(parentIds);
        node2.setFlowAuditValue("userId");
        node2.setFlowAuditType(FlowAuditType.DM);
        node2.setNextIds(List.of(400L));
        nodes.add(node2);


        FlowNodeTemplate node3 = new FlowNodeTemplate();
        node3.setId(300L);
        node3.setFlowAuditValue("1userId");
        node3.setFlowTemplateId(500L);
        node3.setFlowNodeName("3开始");
        node3.setCurrentCondition("OR");
        node3.setParentIds(parentIds);
        node3.setFlowAuditValue("userId");
        node3.setFlowAuditType(FlowAuditType.DM);
        node3.setNextIds(List.of(400L));
        nodes.add(node3);


        FlowNodeTemplate node4 = new FlowNodeTemplate();
        node4.setId(400L);
        node4.setFlowAuditValue("1userId");
        node4.setFlowTemplateId(500L);
        node4.setFlowNodeName("4结束");
        node4.setCurrentCondition("OR");
        node4.setParentIds(childIds);
        node4.setFlowAuditValue("userId");
        node4.setFlowAuditType(FlowAuditType.DM);

        nodes.add(node4);

        flowTemplateDTO.setNodeTemplates(nodes);

        System.out.println(JSON.toJSONString(flowTemplateDTO));
    }


}
